package nc.jv.Sort;

import java.util.Random;

public class BogoSorter implements Sorter
{

	@SuppressWarnings("rawtypes")
	@Override
	public void sort(Comparable[] array)
	{
		if (array == null)
			throw new NullPointerException();
		if (array.length-1 <= 0)
			return;
		
		Random random = new Random();
		while (!isSorted(array))
			for (int i = 0; i < array.length; i++)
				xchg(array, i, random.nextInt(array.length));
	}

	@SuppressWarnings("rawtypes")
	private void xchg(Comparable[] array, int ind1, int ind2)
	{
		Comparable temp = array[ind1];
		array[ind1] = array[ind2];
		array[ind2] = temp;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private boolean isSorted(Comparable[] array)
	{
		for (int i = 0; i < array.length-1; i++)
			if (array[i].compareTo(array[i+1]) > 0)
				return false;
		return true;
	}
	
}
